﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;


namespace DAL
{
    public class AdministratorService
    {
        public static Administrator AddAdministrator(Administrator admin)
        {
            string sql = string.Format("insert into Administrator(PersonalInformationId) values({0});select @@IDENTITY;", admin.PersonalInformationId);
            int id = DBHelper.GetScalar(sql);
            return GetAdministratorById(id);
        }

        public static int GetPersonalInformationIdById(int id)
        {
            Administrator admin = GetAdministratorById(id);
            if (admin == null)
                return -1;
            else
                return admin.PersonalInformationId;
        }

        public static Administrator GetAdministratorById(int id)
        {
            string sql = string.Format("select * from Administrator where Id={0}", id);
            return GetAdministratorBySql(sql);
        }

        private static Administrator GetAdministratorBySql(string sql)
        {
            List<Administrator> list = GetAdministratorsBySql(sql);
            if (list.Count() > 0)
                return list[0];
            return null;
        }

        private static List<Administrator> GetAdministratorsBySql(string sql)
        {
            List<Administrator> list = new List<Administrator>();
            DataTable dt = DBHelper.GetDataSet(sql);
            foreach (DataRow row in dt.Rows)
            {
                Administrator admin = new Administrator();
                admin.Id = (int)row["Id"];
                admin.PersonalInformationId = (int)row["PersonalInformationId"];
                list.Add(admin);
            }
            return list;
        }

        public static Administrator GetAdminByPersonalInformationId(int pId)
        {
            string sql = string.Format("select * from Administrator where PersonalInformationId={0}", pId);
            return GetAdministratorBySql(sql);
        }
    }

}
